/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ElevatorShaft.java
 *
 * Created on 01/09/2011, 2:07:46 PM
 */
package liftsimulator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

//object declaration
/**
 *
 * @author Michael Peever
 */
public class LiftShaft extends javax.swing.JPanel implements Runnable {

    int shaftNo;
    int callerYPos = 5;
    public Lift e;
    ArrayList<Integer> floors;
    int noFloors;
    public ElevatorUI eui;
    public LiftDoor ld = new LiftDoor();
    public Building b;

    /** Creates new form ElevatorShaft */
    public LiftShaft(int shaftNo, int noFloors, int shaftWidth, int floorHeight, Building b) {
        this.noFloors = noFloors;
        this.shaftNo = shaftNo;
        this.b = b;

        floors = new ArrayList<Integer>();
        e = new Lift(callerYPos);
        //eui = new ElevatorUI(noFloors,e);
        int t = 5;
        for (int i = 0; i < noFloors; i++) {
            floors.add(0, t);
            t += 50;
        }

        initComponents();


        this.setName(Integer.toString(shaftNo));
        this.setPreferredSize(new Dimension(shaftWidth, noFloors * floorHeight));
        this.noFloors = noFloors;
    }

    public void callElevator(int index) {
        this.callerYPos = (int) floors.get(index);
    }

    public String getLiftDirection() {
        return e.getDirection();
    }

    public int getLiftFloor() {
        return floors.indexOf(e.liftCurrentYPos) + 1;
    }

    public String getLiftStatus() {
        return e.getStatus();
    }

    public void paint(Graphics g) {
        if (b.liftShaftColour.equals("Orange")) {
            g.setColor(Color.ORANGE);
        }
        if (b.liftShaftColour.equals("Purple")) {
            g.setColor(Color.MAGENTA);
        }
        if (b.liftShaftColour.equals("Yellow")) {
            g.setColor(Color.YELLOW);
        }
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        if (b.liftColour.equals("Black")) {
            g.setColor(Color.BLACK);
        }
        if (b.liftColour.equals("Blue")) {
            g.setColor(Color.BLUE);
        }
        if (b.liftColour.equals("Pink")) {
            g.setColor(Color.PINK);
        }

        g.fillRect(5, e.getLiftCurrentYPos(), e.getWidth(), e.getHeight());
        if (e.getStatus() == "READY") {
            //slowly close lift door
            g.setColor(Color.WHITE);
            g.fillRect(15, e.getLiftCurrentYPos() + 5, e.getWidth() - 20, e.getHeight() - 10);
            if (b.eui != null) {
                for (int i = 0; i < b.shafts.length; i++) {
                    b.euiList.get(i).updateDoorStatusPanel(b.shafts[i].e.getStatus());
                    b.euiList.get(i).updateFloorNumLbl(b.shafts[i].e.getLiftCurrentYPos());
                }
            }
        }
        if (e.getStatus() == "OPENING") {
            g.setColor(Color.WHITE);
            g.fillRect(15, e.getLiftCurrentYPos() + 5, ld.getCurrentWidth(), e.getHeight() - 10);
            if (b.eui != null) {
                for (int i = 0; i < b.shafts.length; i++) {
                    b.euiList.get(i).updateDoorStatusPanel(b.shafts[i].e.getStatus());
                    b.euiList.get(i).updateFloorNumLbl(b.shafts[i].e.getLiftCurrentYPos());
                }
            }
        }
        if (e.getStatus() == "CLOSING") {
            g.setColor(Color.WHITE);
            g.fillRect(15, e.getLiftCurrentYPos() + 5, ld.getCurrentWidth(), e.getHeight() - 10);
            if (b.eui != null) {
                for (int i = 0; i < b.shafts.length; i++) {
                    b.euiList.get(i).updateDoorStatusPanel(b.shafts[i].e.getStatus());
                    b.euiList.get(i).updateFloorNumLbl(b.shafts[i].e.getLiftCurrentYPos());
                }
            }

            if (ld.getCurrentWidth() == 0) {
                e.setIsDoorsClosed(true);
            }

        }


    }

    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(30);
                if ((int) this.callerYPos < (int) e.getLiftCurrentYPos()) {
                    if (!e.isDoorsClosed()) {
                        e.setStatus("CLOSING");
                        ld.setCurrentWidth(ld.getCurrentWidth() - 1);
                    }
                    if (e.isDoorsClosed) {
                        e.setLiftCurrentYPos(e.getLiftCurrentYPos() - 1);
                        e.setStatus("MOVING");
                        for (int i = 0; i < b.shafts.length; i++) {
                            b.euiList.get(i).updateDoorStatusPanel(b.shafts[i].e.getStatus());
                            b.euiList.get(i).updateFloorNumLbl(b.shafts[i].e.getLiftCurrentYPos());
                        }

                    }
                }

                if ((int) this.callerYPos > (int) e.getLiftCurrentYPos()) {
                    if (!e.isDoorsClosed()) {
                        e.setStatus("CLOSING");
                        ld.setCurrentWidth(ld.getCurrentWidth() - 1);
                    }
                    if (e.isDoorsClosed) {
                        e.setLiftCurrentYPos(e.getLiftCurrentYPos() + 1);
                        e.setStatus("MOVING");
                        for (int i = 0; i < b.shafts.length; i++) {
                            b.euiList.get(i).updateDoorStatusPanel(b.shafts[i].e.getStatus());
                            b.euiList.get(i).updateFloorNumLbl(b.shafts[i].e.getLiftCurrentYPos());
                        }
                    }
                }

                if ((int) this.callerYPos == (int) e.getLiftCurrentYPos()) {
                    if (!(e.getStatus() == "READY")) {
                        ld.setCurrentWidth(ld.getCurrentWidth() + 1);
                        e.setStatus("OPENING");
                    }
                    if (ld.getCurrentWidth() == e.getWidth() - 20) {
                        e.setIsDoorsClosed(false);
                        e.setStatus("READY");
                    }


                }

            } catch (Exception trte) {
            }
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jLabel1.setText("Test");

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 51, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 259, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
